import warnings
warnings.warn("qturboframe.public_ui.ufc_wpf.list_item模块已被废弃，替代模块为qturboframe.public_ui.ucf_wpf.list_item", DeprecationWarning)

from datetime import datetime
from qturboframe.lib.utils.by import By
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.public_ui.ufc_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ufc_wpf.utili.common import Common


class ListItem(BaseControl):
    """
    ucf wpf列表

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - list_items: 当前的list_items对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: ListItem({By.TEXT: 'glrs'})
     - 例2: ListItem({By.AUTOMATIONID:'automationid'})
     - 例3: ListItem(control)
     - 例4: ListItem({By.TEXT: 'glrs'}, father=client)
     - 例5: ListItem({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: ListItem(control, father=client)
    """

    def __init__(self, element_para, father=None, timeout=10000):
        self.text = ""
        if isinstance(element_para, dict) and By.TEXT in element_para:
            text = element_para.get(By.TEXT)
            text = Common.update_special_character(text)
            self.text = text
            print("list_item 中查找listitem的开始时间", datetime.now())
            xpath = ".//listitem//text[@text~'{}']".format(text)
            controls = father.find_elements_by_attribute(By.XPATH, xpath, timeout=timeout)
            print("list_item 中查找listitem的开始时间", datetime.now())
            self.list_items = []
            if len(controls)!=0:
                self._driver = father
                self.timeout = timeout
                for control in controls:
                    xpath = ".//ancestor::listitem"
                    temp = control.find_element_by_attribute(By.XPATH, xpath, timeout = timeout)
                    self.list_items.append(temp)
            else:
                raise CsClientError("list中没有找到此控件:{}".format(element_para))
        else:
            super().__init__(element_para, father, timeout)
            self.list_items = self.controls

    def select_item(self, texts=[]):
        """
        wpf ucf 选择符合要求的list项

        :Args:
         - texts: 选择项的文本 - list类型 - 有的下拉框是由多个内容 用空格分隔的- 传入内容可能为['600582','恒生电子'，'深圳']

        :Raise:
         - CsClientError 传入的texts为空列表但是找到了多个list项抛出错误 - 因为无区分要选择哪一个list

        :Example:
         - 例1: list_item.select_item()
         - 例2: list_item.select_item([1,2])
        """
        first_text = texts[0]
        last_texts = texts[1:]
        if len(last_texts) == 0 and len(self.list_items) == 1:
            self.list_items[0].focus()
            self.list_items[0].click()
        elif len(last_texts) == 0 and len(self.list_items) > 1:
            #找到多个符合要求的下拉框，
            for list_item in self.list_items:
                text_control = list_item.find_element_by_attribute(By.XPATH,".//text[@Type='TextBlock']", timeout=1000)
                if text_control.get_text() == first_text:
                    list_item.focus()
                    list_item.click()
                    break
            else:
                raise CsClientError("没有找到文本为：'{}'的下拉内容".format(first_text))
        else:
            for list_item in self.list_items:
                num = 0
                for text in last_texts:
                    control = list_item.is_element_exist(".//text[@text~'{}']".format(text.strip()), time=1000)
                    if control:
                        num +=1
                        continue
                    else:
                        break

                if num == len(last_texts):
                    #用double click是因为宽途产品的“菜单/组件”中出现的下拉菜单需要双击
                    #但是目前在别的地方验证用double click也没问题
                    #已经验证了多选的情况
                    list_item.double_click()
                    break